﻿using UnityEngine;
using System.Collections;
using Clojure;
using PBMessage;
public class AbiliAttack : Ability
{
    public override void Init(Unit unit)
    {
        base.Init(unit);
        PlayerModel player = Game.Model.Get<PlayerModel>();
        if (unit.Id == player.UserName)
        {
            Game.Event.AddHandler("UnitClick", OnUnitClick);
        }
    }

    public void OnUnitClick(object param0, object param1)
    {
        GameScene scene = Game.Scene.Cur<GameScene>();
        Unit TargetUnit = (Unit)param1;
        if (TargetUnit.IsDie) 
        {
            return; 
        }
        GamePlayer gamePlayer = scene.GetGamePlayer(TargetUnit.GamePlayerId);
        float distance =Vector3.Distance(this.mUnit.gameObject.transform.localPosition,TargetUnit.transform.localPosition);
        if (gamePlayer.Camp != scene.LocalPlayer.Camp) 
        {
            if (this.mUnit.GetAttackRange() >= distance)
            {
                this.ExecAttack(TargetUnit);
            }
            else 
            {
                Vector3 DisNormal = (this.mUnit.transform.localPosition-TargetUnit.transform.localPosition);
                DisNormal.Normalize();
                Vector3 MovePos = TargetUnit.transform.localPosition + DisNormal * this.mUnit.GetAttackRange();

                BaseCommand cmdMove = this.mUnit.CmdQueue.Push(CommandType.Unit_Move, MovePos.x, MovePos.y, MovePos.z);
                Game.Packet.SendPushUnitCommand(this.mUnit.Id, CommandType.Unit_Move, PacketHelper.PushCmdUnitType.ClearPush, cmdMove.Args);
                BaseCommand cmdAttack = this.mUnit.CmdQueue.Push(CommandType.Unit_Attack,TargetUnit.Id);
                Game.Packet.SendPushUnitCommand(this.mUnit.Id, CommandType.Unit_Attack, PacketHelper.PushCmdUnitType.Push, cmdAttack.Args);
            } 
        }
    }

    public void ExecAttack(Unit target) 
    {
        for (int i = 0; i < this.mUnit.Weapons.Count; i++)
        {
            Weapon weapon = this.mUnit.Weapons[i];
            if (weapon.AttackCD == weapon.AttackSpeed)
            {
                BaseCommand cmd = this.mUnit.CmdQueue.Push(CommandType.Unit_Attack, target.Id);
                Game.Packet.SendPushUnitCommand(this.mUnit.Id, CommandType.Unit_Attack, PacketHelper.PushCmdUnitType.ClearPush, cmd.Args);
                weapon.AttackCD = 0;
            }
        }
    }
    public void Update() 
    {
        for (int i = 0; i < this.mUnit.Weapons.Count;i++) 
        {
            Weapon weapon = this.mUnit.Weapons[i];
            if (weapon.AttackCD < weapon.AttackSpeed)
            {
                weapon.AttackCD += Time.deltaTime;
            }
            else if (weapon.AttackCD > weapon.AttackSpeed) 
            {
                weapon.AttackCD = weapon.AttackSpeed;
            }
        }
    }

    public void ServerRetAttack(SC_AbilityAttack PacketData) 
    {
        this.mUnit.CallEvent("attack01");
        Unit targetUnit = UnitManager.Instance.GetUnit(PacketData.TargetUnitID);
        if (targetUnit != null)
        {
            transform.LookAt(targetUnit.transform);
            this.mUnit.Weapons[PacketData.WeaponId].ExecAttack(targetUnit);
        }
    }

}
